package com.example.javaairportflightanalysis.common;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class CrawAllApcd {
    public static void main(String[] args) {
        // 数据库连接参数
        String url = "jdbc:dm://localhost:5237?schema=ZBTJ";
        String user = "SYSDBA";
        String password = "SYSDBA";
        String filePath="C:\\Users\\shen\\Desktop\\apcd.docx";

        File file = new File(filePath);
        XWPFDocument document = null;

        try {
            document = new XWPFDocument(new FileInputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            Class.forName("dm.jdbc.driver.DmDriver");
            connection = DriverManager.getConnection(url, user, password);
            statement = connection.prepareStatement("insert into zbtj.ALLAPCD(code, cnnm) values(?, ?)");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 读取Word文档中的段落内容
        for (XWPFParagraph paragraph : document.getParagraphs()) {
            // 获取每一行的文本
            String text = paragraph.getText();
            if (text.split(" ").length > 1) {
                // 分割三字码和中文名
                String[] parts = text.split(" ");
                // 去掉空格
                String code = parts[0].trim();
                String cnnm = parts[1].trim();
                // 打印
                System.out.println(code + " " + cnnm);
                // 设置参数并执行插入操作
            try {
                if (statement != null) {
                    statement.setString(1, code);
                    statement.setString(2, cnnm);
                    statement.executeUpdate();
                }else {
                    System.out.println("statement is null");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            }

        }
    }
}
